
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>[k8s] 第七章 Service详解</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p>本章节主要介绍kubernetes的流量负载组件：Service和Ingress。</p> 
<h3><a id="Service_3"></a>Service介绍</h3> 
<p>​ 在kubernetes中，pod是应用程序的载体，我们可以通过pod的ip来访问应用程序，但是pod的ip地址不是固定的，这也就意味着不方便直接采用pod的ip对服务进行访问。</p> 
<p>​ 为了解决这个问题，kubernetes提供了Service资源，Service会对提供同一个服务的多个pod进行聚合，并且提供一个统一的入口地址。通过访问Service的入口地址就能访问到后面的pod服务。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/5eeab547bfecbeb207c8c2e9ca70b2ac.png#pic_center" alt="在这里插入图片描述"></p> 
<p>​ Service在很多情况下只是一个概念，真正起作用的其实是kube-proxy服务进程，每个Node节点上都运行着一个kube-proxy服务进程。当创建Service的时候会通过api-server向etcd写入创建的service的信息，而kube-proxy会基于监听的机制发现这种Service的变动，然后<strong>它会将最新的Service信息转换成对应的访问规则</strong>。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/4717925e194d5c8dd9d68faa7d2fd510.png" alt=""></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 10.97.97.97:80 是service提供的访问入口</span>
<span class="token comment"># 当访问这个入口的时候，可以发现后面有三个pod的服务在等待调用，</span>
<span class="token comment"># kube-proxy会基于rr（轮询）的策略，将请求分发到其中一个pod上去</span>
<span class="token comment"># 这个规则会同时在集群内的所有节点上都生成，所以在任何一个节点上访问都可以。</span>
<span class="token namespace">[root@node1 ~]</span><span class="token comment"># ipvsadm -Ln</span>
IP Virtual Server version 1<span class="token punctuation">.</span>2<span class="token punctuation">.</span>1 <span class="token punctuation">(</span>size=4096<span class="token punctuation">)</span>
Prot LocalAddress:Port Scheduler Flags
  <span class="token operator">-</span>&gt; RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97:80 rr
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80               Masq    1      0          0
</code></pre> 
<p>kube-proxy目前支持三种工作模式:</p> 
<p><strong>userspace 模式</strong></p> 
<p>​ userspace模式下，kube-proxy会为每一个Service创建一个监听端口，发向Cluster IP的请求被Iptables规则重定向到kube-proxy监听的端口上，kube-proxy根据LB算法选择一个提供服务的Pod并和其建立链接，以将请求转发到Pod上。<br> ​ 该模式下，kube-proxy充当了一个四层负责均衡器的角色。由于kube-proxy运行在userspace中，在进行转发处理时会增加内核和用户空间之间的数据拷贝，虽然比较稳定，但是效率比较低。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/b6c11bd15732f5be57c004deb8d7b435.png" alt=""></p> 
<p><strong>iptables 模式</strong></p> 
<p>​ iptables模式下，kube-proxy为service后端的每个Pod创建对应的iptables规则，直接将发向Cluster IP的请求重定向到一个Pod IP。<br> ​ 该模式下kube-proxy不承担四层负责均衡器的角色，只负责创建iptables规则。该模式的优点是较userspace模式效率更高，但不能提供灵活的LB策略，当后端Pod不可用时也无法进行重试。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/e6e3a5f28f8d298a8db32fd98c6f12fd.png" alt=""></p> 
<p><strong>ipvs 模式</strong></p> 
<p>​ ipvs模式和iptables类似，kube-proxy监控Pod的变化并创建相应的ipvs规则。ipvs相对iptables转发效率更高。除此以外，ipvs支持更多的LB算法。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/4ec84969a943f69aacaaeb056eb943ca.png" alt=""></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 此模式必须安装ipvs内核模块，否则会降级为iptables</span>
<span class="token comment"># 开启ipvs</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl edit cm kube-proxy -n kube-system # 查找 mode:"" 更改为 mode:"ipvs"</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete pod -l k8s-app=kube-proxy -n kube-system</span>
<span class="token namespace">[root@node1 ~]</span><span class="token comment"># ipvsadm -Ln</span>
IP Virtual Server version 1<span class="token punctuation">.</span>2<span class="token punctuation">.</span>1 <span class="token punctuation">(</span>size=4096<span class="token punctuation">)</span>
Prot LocalAddress:Port Scheduler Flags
  <span class="token operator">-</span>&gt; RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97:80 rr
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80               Masq    1      0          0
</code></pre> 
<h3><a id="Service_68"></a>Service类型</h3> 
<p>Service的资源清单文件：</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">kind</span><span class="token punctuation">:</span> Service  <span class="token comment"># 资源类型</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1  <span class="token comment"># 资源版本</span>
<span class="token key atrule">metadata</span><span class="token punctuation">:</span> <span class="token comment"># 元数据</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> service <span class="token comment"># 资源名称</span>
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev <span class="token comment"># 命名空间</span>
<span class="token key atrule">spec</span><span class="token punctuation">:</span> <span class="token comment"># 描述</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span> <span class="token comment"># 标签选择器，用于确定当前service代理哪些pod</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">type</span><span class="token punctuation">:</span> <span class="token comment"># Service类型，指定service的访问方式</span>
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span>  <span class="token comment"># 虚拟服务的ip地址</span>
  <span class="token key atrule">sessionAffinity</span><span class="token punctuation">:</span> <span class="token comment"># session亲和性，支持ClientIP、None两个选项</span>
  <span class="token key atrule">ports</span><span class="token punctuation">:</span> <span class="token comment"># 端口信息</span>
    <span class="token punctuation">-</span> <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP 
      <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3017</span>  <span class="token comment"># service端口</span>
      <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">5003</span> <span class="token comment"># pod端口</span>
      <span class="token key atrule">nodePort</span><span class="token punctuation">:</span> <span class="token number">31122</span> <span class="token comment"># 主机端口</span>
</code></pre> 
<h4><a id="type__Service_90"></a>type: # Service类型</h4> 
<ul><li>ClusterIP：默认值，它是Kubernetes系统自动分配的虚拟IP，只能在集群内部访问</li><li>NodePort：将Service通过指定的Node上的端口暴露给外部，通过此方法，就可以在集群外部访问服务</li><li>LoadBalancer：使用外接负载均衡器完成到服务的负载分发，注意此模式需要外部云环境支持</li><li>ExternalName： 把集群外部的服务引入集群内部，直接使用</li></ul> 
<h3><a id="Service_96"></a>Service使用</h3> 
<h4><a id="_98"></a>实验环境准备</h4> 
<p>在使用service之前，首先利用Deployment创建出3个pod，注意要为pod设置<code>app=nginx-pod</code>的标签</p> 
<p>创建deployment.yaml，内容如下：</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment      
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> pc<span class="token punctuation">-</span>deployment
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span> 
  <span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">3</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
        <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>1.17.1
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f deployment.yaml</span>
deployment<span class="token punctuation">.</span>apps/pc-deployment created

<span class="token comment"># 查看pod详情</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev -o wide --show-labels</span>
NAME                             READY   STATUS     IP            NODE     LABELS
pc-deployment-66cb59b984-8p84h   1/1     Running    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40   node1    app=nginx-pod
pc-deployment-66cb59b984-vx8vx   1/1     Running    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33   node2    app=nginx-pod
pc-deployment-66cb59b984-wnncx   1/1     Running    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39   node1    app=nginx-pod

<span class="token comment"># 为了方便后面的测试，修改下三台nginx的index.html页面（三台修改的IP地址不一致）</span>
<span class="token comment"># kubectl exec -it pc-deployment-66cb59b984-8p84h -n dev /bin/sh</span>
<span class="token comment"># echo "10.244.1.40" &gt; /usr/share/nginx/html/index.html</span>

<span class="token comment">#修改完毕之后，访问测试</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl 10.244.1.40</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl 10.244.2.33</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl 10.244.1.39</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39
</code></pre> 
<h4><a id="ClusterIPService_151"></a>ClusterIP类型的Service</h4> 
<p>创建service-clusterip.yaml文件</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> service<span class="token punctuation">-</span>clusterip
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span> 10.97.97.97 <span class="token comment"># service的ip地址，如果不写，默认会生成一个</span>
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ClusterIP
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>  <span class="token comment"># Service端口       </span>
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span> <span class="token comment"># pod端口</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f service-clusterip.yaml</span>
service/service-clusterip created

<span class="token comment"># 查看service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc -n dev -o wide</span>
NAME                <span class="token function">TYPE</span>        CLUSTER-IP    EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>   AGE   SELECTOR
service-clusterip   ClusterIP   10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97   &lt;none&gt;        80/TCP    13s   app=nginx-pod

<span class="token comment"># 查看service的详细信息</span>
<span class="token comment"># 在这里有一个Endpoints列表，里面就是当前service可以负载到的服务入口</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe svc service-clusterip -n dev</span>
Name:              service-clusterip
Namespace:         dev
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=nginx-pod
<span class="token function">Type</span>:              ClusterIP
IP:                10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97
Port:              &lt;unset&gt;  80/TCP
TargetPort:        80/TCP
Endpoints:         10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80
Session Affinity:  None
Events:            &lt;none&gt;

<span class="token comment"># 查看ipvs的映射规则</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># ipvsadm -Ln</span>
TCP  10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97:80 rr
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80               Masq    1      0          0

<span class="token comment"># 访问10.97.97.97:80观察效果</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl 10.97.97.97:80</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
</code></pre> 
<p><strong>Endpoint</strong></p> 
<p>​ Endpoint是kubernetes中的一个资源对象，存储在etcd中，用来记录一个service对应的所有pod的访问地址，它是根据service配置文件中selector描述产生的。</p> 
<p>​ 一个Service由一组Pod组成，这些Pod通过Endpoints暴露出来，<strong>Endpoints是实现实际服务的端点集合</strong>。换句话说，service和pod之间的联系是通过endpoints实现的。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/9817c3b96247983565507bb18d7b68e4.png" alt="image-20200509191917069"></p> 
<p><strong>负载分发策略</strong></p> 
<p>对Service的访问被分发到了后端的Pod上去，目前kubernetes提供了两种负载分发策略：</p> 
<ul><li> <p>如果不定义，默认使用kube-proxy的策略，比如随机、轮询</p> </li><li> <p>基于客户端地址的会话保持模式，即来自同一个客户端发起的所有请求都会转发到固定的一个Pod上</p> <p>此模式可以使在spec中添加<code>sessionAffinity:ClientIP</code>选项</p> </li></ul> 
<pre><code class="prism language-powershell"><span class="token comment"># 查看ipvs的映射规则【rr 轮询】</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># ipvsadm -Ln</span>
TCP  10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97:80 rr
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80               Masq    1      0          0

<span class="token comment"># 循环访问测试</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># while true;do curl 10.97.97.97:80; sleep 5; done;</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33

<span class="token comment"># 修改分发策略----sessionAffinity:ClientIP</span>

<span class="token comment"># 查看ipvs规则【persistent 代表持久】</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># ipvsadm -Ln</span>
TCP  10<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97<span class="token punctuation">.</span>97:80 rr persistent 10800
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80               Masq    1      0          0
  <span class="token operator">-</span>&gt; 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80               Masq    1      0          0

<span class="token comment"># 循环访问测试</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># while true;do curl 10.97.97.97; sleep 5; done;</span>
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
  
<span class="token comment"># 删除service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete -f service-clusterip.yaml</span>
service <span class="token string">"service-clusterip"</span> deleted
</code></pre> 
<h4><a id="HeadLinessService_264"></a>HeadLiness类型的Service</h4> 
<p>​ 在某些场景中，开发人员可能不想使用Service提供的负载均衡功能，而希望自己来控制负载均衡策略，针对这种情况，kubernetes提供了HeadLiness Service，这类Service不会分配Cluster IP，如果想要访问service，只能通过service的域名进行查询。</p> 
<p>创建service-headliness.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> service<span class="token punctuation">-</span>headliness
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span> None <span class="token comment"># 将clusterIP设置为None，即可创建headliness Service</span>
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ClusterIP
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>    
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f service-headliness.yaml</span>
service/service-headliness created

<span class="token comment"># 获取service， 发现CLUSTER-IP未分配</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc service-headliness -n dev -o wide</span>
NAME                 <span class="token function">TYPE</span>        CLUSTER-IP   EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>   AGE   SELECTOR
service-headliness   ClusterIP   None         &lt;none&gt;        80/TCP    11s   app=nginx-pod

<span class="token comment"># 查看service详情</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe svc service-headliness  -n dev</span>
Name:              service-headliness
Namespace:         dev
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=nginx-pod
<span class="token function">Type</span>:              ClusterIP
IP:                None
Port:              &lt;unset&gt;  80/TCP
TargetPort:        80/TCP
Endpoints:         10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33:80
Session Affinity:  None
Events:            &lt;none&gt;

<span class="token comment"># 查看域名的解析情况</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl exec -it pc-deployment-66cb59b984-8p84h -n dev /bin/sh</span>
<span class="token operator">/</span> <span class="token comment"># cat /etc/resolv.conf</span>
nameserver 10<span class="token punctuation">.</span>96<span class="token punctuation">.</span>0<span class="token punctuation">.</span>10
search dev<span class="token punctuation">.</span>svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local cluster<span class="token punctuation">.</span>local

<span class="token namespace">[root@master ~]</span><span class="token comment"># dig @10.96.0.10 service-headliness.dev.svc.cluster.local</span>
service-headliness<span class="token punctuation">.</span>dev<span class="token punctuation">.</span>svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local<span class="token punctuation">.</span> 30 IN A 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>40
service-headliness<span class="token punctuation">.</span>dev<span class="token punctuation">.</span>svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local<span class="token punctuation">.</span> 30 IN A 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>39
service-headliness<span class="token punctuation">.</span>dev<span class="token punctuation">.</span>svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local<span class="token punctuation">.</span> 30 IN A 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>33
</code></pre> 
<h4><a id="NodePortService_323"></a>NodePort类型的Service</h4> 
<p>​ 在之前的样例中，创建的Service的ip地址只有集群内部才可以访问，如果希望将Service暴露给集群外部使用，那么就要使用到另外一种类型的Service，称为NodePort类型。NodePort的工作原理其实就是<strong>将service的端口映射到Node的一个端口上</strong>，然后就可以通过<code>NodeIp:NodePort</code>来访问service了。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/caecf60e49fa5a48abd600f251203b86.png" alt=""></p> 
<p>创建service-nodeport.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> service<span class="token punctuation">-</span>nodeport
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">type</span><span class="token punctuation">:</span> NodePort <span class="token comment"># service类型</span>
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
    <span class="token key atrule">nodePort</span><span class="token punctuation">:</span> <span class="token number">30002</span> <span class="token comment"># 指定绑定的node的端口(默认的取值范围是：30000-32767), 如果不指定，会默认分配</span>
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f service-nodeport.yaml</span>
service/service-nodeport created

<span class="token comment"># 查看service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc -n dev -o wide</span>
NAME               <span class="token function">TYPE</span>       CLUSTER-IP      EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>       SELECTOR
service-nodeport   NodePort   10<span class="token punctuation">.</span>105<span class="token punctuation">.</span>64<span class="token punctuation">.</span>191   &lt;none&gt;        80:30002/TCP  app=nginx-pod

<span class="token comment"># 接下来可以通过电脑主机的浏览器去访问集群中任意一个nodeip的30002端口，即可访问到pod</span>
</code></pre> 
<h4><a id="LoadBalancerService_360"></a>LoadBalancer类型的Service</h4> 
<p>​ LoadBalancer和NodePort很相似，目的都是向外部暴露一个端口，区别在于LoadBalancer会在集群的外部再来做一个负载均衡设备，而这个设备需要外部环境支持的，外部服务发送到这个设备上的请求，会被设备负载之后转发到集群中。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/9b5bfc24919a38fb7647325eb9d1afc4.png" alt=""></p> 
<h4><a id="ExternalNameService_366"></a>ExternalName类型的Service</h4> 
<p>​ ExternalName类型的Service用于引入集群外部的服务，它通过<code>externalName</code>属性指定外部一个服务的地址，然后在集群内部访问此service就可以访问到外部的服务了。</p> 
<blockquote> 
 <p>说明： ExternalName 接受 IPv4 地址字符串，但作为包含数字的 DNS 名称，而不是 IP 地址。 类似于 IPv4 地址的外部名称不能由 CoreDNS 或 ingress-nginx 解析，因为外部名称旨在指定规范的 DNS 名称。 要对 IP 地址进行硬编码，请考虑使用 <a href="#headless-services" rel="nofollow">headless Services</a>。</p> 
</blockquote> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/8bf74a383b95fe399af5a5644cfb2c95.png" alt=""></p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> service<span class="token punctuation">-</span>externalname
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ExternalName <span class="token comment"># service类型</span>
  <span class="token key atrule">externalName</span><span class="token punctuation">:</span> www.baidu.com  <span class="token comment">#改成ip地址也可以</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl  create -f service-externalname.yaml</span>
service/service-externalname created

<span class="token comment"># 域名解析</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># dig @10.96.0.10 service-externalname.dev.svc.cluster.local</span>
service-externalname<span class="token punctuation">.</span>dev<span class="token punctuation">.</span>svc<span class="token punctuation">.</span>cluster<span class="token punctuation">.</span>local<span class="token punctuation">.</span> 30 IN CNAME www<span class="token punctuation">.</span>baidu<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
www<span class="token punctuation">.</span>baidu<span class="token punctuation">.</span>com<span class="token punctuation">.</span>          30      IN      CNAME   www<span class="token punctuation">.</span>a<span class="token punctuation">.</span>shifen<span class="token punctuation">.</span>com<span class="token punctuation">.</span>
www<span class="token punctuation">.</span>a<span class="token punctuation">.</span>shifen<span class="token punctuation">.</span>com<span class="token punctuation">.</span>       30      IN      A       39<span class="token punctuation">.</span>156<span class="token punctuation">.</span>66<span class="token punctuation">.</span>18
www<span class="token punctuation">.</span>a<span class="token punctuation">.</span>shifen<span class="token punctuation">.</span>com<span class="token punctuation">.</span>       30      IN      A       39<span class="token punctuation">.</span>156<span class="token punctuation">.</span>66<span class="token punctuation">.</span>14
</code></pre> 
<h3><a id="Ingress_399"></a>Ingress介绍</h3> 
<p>​ 在前面课程中已经提到，Service对集群之外暴露服务的主要方式有两种：NotePort和LoadBalancer，但是这两种方式，都有一定的缺点：</p> 
<ul><li>NodePort方式的缺点是会占用很多集群机器的端口，那么当集群服务变多的时候，这个缺点就愈发明显</li><li>LB方式的缺点是每个service需要一个LB，浪费、麻烦，并且需要kubernetes之外设备的支持</li></ul> 
<p>​ 基于这种现状，kubernetes提供了Ingress资源对象，Ingress只需要一个NodePort或者一个LB就可以满足暴露多个Service的需求。工作机制大致如下图表示：</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/59071e44c04c4ac6c603e07be58ae6b7.png" alt=""></p> 
<p>​ 实际上，Ingress相当于一个7层的负载均衡器，是kubernetes对反向代理的一个抽象，它的工作原理类似于Nginx，可以理解成在<strong>Ingress里建立诸多映射规则，Ingress Controller通过监听这些配置规则并转化成Nginx的反向代理配置 , 然后对外部提供服务</strong>。在这里有两个核心概念：</p> 
<ul><li>ingress：kubernetes中的一个对象，作用是定义请求如何转发到service的规则</li><li>ingress controller：具体实现反向代理及负载均衡的程序，对ingress定义的规则进行解析，根据配置的规则来实现请求转发，实现方式有很多，比如Nginx, Contour, Haproxy等等</li></ul> 
<p>Ingress（以Nginx为例）的工作原理如下：</p> 
<ol><li>用户编写Ingress规则，说明哪个域名对应kubernetes集群中的哪个Service</li><li>Ingress控制器动态感知Ingress服务规则的变化，然后生成一段对应的Nginx反向代理配置</li><li>Ingress控制器会将生成的Nginx配置写入到一个运行着的Nginx服务中，并动态更新</li><li>到此为止，其实真正在工作的就是一个Nginx了，内部配置了用户定义的请求转发规则</li></ol> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/50b779190b8be6a55d344b6c8c6f3e60.png" alt=""></p> 
<h3><a id="Ingress_424"></a>Ingress使用</h3> 
<h4><a id="_426"></a>环境准备</h4> 
<p><strong>搭建ingress环境</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建文件夹</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># mkdir ingress-controller</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># cd ingress-controller/</span>

<span class="token comment"># 获取ingress-nginx，本次案例使用的是0.30版本</span>
<span class="token namespace">[root@master ingress-controller]</span><span class="token comment"># wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml</span>
<span class="token namespace">[root@master ingress-controller]</span><span class="token comment"># wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml</span>

<span class="token comment"># 修改mandatory.yaml文件中的仓库</span>
<span class="token comment"># 修改quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0</span>
<span class="token comment"># 为quay-mirror.qiniu.com/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0</span>
<span class="token comment"># 创建ingress-nginx</span>
<span class="token namespace">[root@master ingress-controller]</span><span class="token comment"># kubectl apply -f ./</span>

<span class="token comment"># 查看ingress-nginx</span>
<span class="token namespace">[root@master ingress-controller]</span><span class="token comment"># kubectl get pod -n ingress-nginx</span>
NAME                                           READY   STATUS    RESTARTS   AGE
pod/nginx-ingress-controller-fbf967dd5-4qpbp   1/1     Running   0          12h

<span class="token comment"># 查看service</span>
<span class="token namespace">[root@master ingress-controller]</span><span class="token comment"># kubectl get svc -n ingress-nginx</span>
NAME            <span class="token function">TYPE</span>       CLUSTER-IP     EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>                      AGE
ingress-nginx   NodePort   10<span class="token punctuation">.</span>98<span class="token punctuation">.</span>75<span class="token punctuation">.</span>163   &lt;none&gt;        80:32240/TCP<span class="token punctuation">,</span>443:31335/TCP   11h
</code></pre> 
<p><strong>准备service和pod</strong></p> 
<p>为了后面的实验比较方便，创建如下图所示的模型</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/57833a7e42b608cea2a77098c7126b05.png" alt=""></p> 
<p>创建tomcat-nginx.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>deployment
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">3</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
        <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>1.17.1
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>

<span class="token punctuation">---</span>

<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>deployment
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">3</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat
        <span class="token key atrule">image</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">:</span>8.5<span class="token punctuation">-</span>jre10<span class="token punctuation">-</span>slim
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>

<span class="token punctuation">---</span>

<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>service
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span> None
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ClusterIP
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span>

<span class="token punctuation">---</span>

<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>service
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>pod
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span> None
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ClusterIP
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8080</span>
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">8080</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f tomcat-nginx.yaml</span>

<span class="token comment"># 查看</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc -n dev</span>
NAME             <span class="token function">TYPE</span>        CLUSTER-IP   EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>    AGE
nginx-service    ClusterIP   None         &lt;none&gt;        80/TCP     48s
tomcat-service   ClusterIP   None         &lt;none&gt;        8080/TCP   48s
</code></pre> 
<h4><a id="Http_553"></a>Http代理</h4> 
<p>创建ingress-http.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> networking.k8s.io/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Ingress
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> ingress<span class="token punctuation">-</span>http
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
  <span class="token key atrule">annotations</span><span class="token punctuation">:</span>
    <span class="token key atrule">kubernetes.io/ingress.class</span><span class="token punctuation">:</span> nginx
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">host</span><span class="token punctuation">:</span> nginx.itheima.com
    <span class="token key atrule">http</span><span class="token punctuation">:</span>
      <span class="token key atrule">paths</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">path</span><span class="token punctuation">:</span> /
      	<span class="token key atrule">pathType</span><span class="token punctuation">:</span> Prefix
        <span class="token key atrule">backend</span><span class="token punctuation">:</span>
          <span class="token key atrule">service</span><span class="token punctuation">:</span>
          	<span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>service
          	<span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
  <span class="token punctuation">-</span> <span class="token key atrule">host</span><span class="token punctuation">:</span> tomcat.itheima.com
    <span class="token key atrule">http</span><span class="token punctuation">:</span>
      <span class="token key atrule">paths</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">path</span><span class="token punctuation">:</span> /
        <span class="token key atrule">pathType</span><span class="token punctuation">:</span> Prefix
        <span class="token key atrule">backend</span><span class="token punctuation">:</span>
          <span class="token key atrule">service</span><span class="token punctuation">:</span>
          	<span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>service
          	<span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8080</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f ingress-http.yaml</span>
ingress<span class="token punctuation">.</span>extensions/ingress-http created

<span class="token comment"># 查看</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get ing ingress-http -n dev</span>
NAME           HOSTS                                  ADDRESS   PORTS   AGE
ingress-http   nginx<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com<span class="token punctuation">,</span>tomcat<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com             80      22s

<span class="token comment"># 查看详情</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe ing ingress-http  -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Rules:
Host                Path  Backends
<span class="token operator">--</span><span class="token operator">--</span>                <span class="token operator">--</span><span class="token operator">--</span>  <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
nginx<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com   <span class="token operator">/</span> nginx-service:80 <span class="token punctuation">(</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>96:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>97:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>112:80<span class="token punctuation">)</span>
tomcat<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com  <span class="token operator">/</span> tomcat-service:8080<span class="token punctuation">(</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>94:8080<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>95:8080<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>111:8080<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token comment"># 接下来,在本地电脑上配置host文件,解析上面的两个域名到192.168.109.100(master)上</span>
<span class="token comment"># 然后,就可以分别访问tomcat.itheima.com:32240  和  nginx.itheima.com:32240 查看效果了</span>
</code></pre> 
<h4><a id="Https_611"></a>Https代理</h4> 
<p>创建证书</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 生成证书</span>
openssl req <span class="token operator">-</span>x509 <span class="token operator">-</span>sha256 <span class="token operator">-</span>nodes <span class="token operator">-</span>days 365 <span class="token operator">-</span>newkey rsa:2048 <span class="token operator">-</span>keyout tls<span class="token punctuation">.</span>key <span class="token operator">-</span>out tls<span class="token punctuation">.</span>crt <span class="token operator">-</span>subj <span class="token string">"/C=CN/ST=BJ/L=BJ/O=nginx/CN=itheima.com"</span>

<span class="token comment"># 创建密钥</span>
kubectl create secret tls tls-secret <span class="token operator">--</span>key tls<span class="token punctuation">.</span>key <span class="token operator">--</span>cert tls<span class="token punctuation">.</span>crt
</code></pre> 
<p>创建ingress-https.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> networking.k8s.io/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Ingress
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> ingress<span class="token punctuation">-</span>https
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
  <span class="token key atrule">annotations</span><span class="token punctuation">:</span>
    <span class="token key atrule">kubernetes.io/ingress.class</span><span class="token punctuation">:</span> nginx
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">tls</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">hosts</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> nginx.itheima.com
      <span class="token punctuation">-</span> tomcat.itheima.com
      <span class="token key atrule">secretName</span><span class="token punctuation">:</span> tls<span class="token punctuation">-</span>secret <span class="token comment"># 指定秘钥</span>
  <span class="token key atrule">rules</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">host</span><span class="token punctuation">:</span> nginx.itheima.com
    <span class="token key atrule">http</span><span class="token punctuation">:</span>
      <span class="token key atrule">paths</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">path</span><span class="token punctuation">:</span> /
        <span class="token key atrule">pathType</span><span class="token punctuation">:</span> Prefix
        <span class="token key atrule">backend</span><span class="token punctuation">:</span>
          <span class="token key atrule">service</span><span class="token punctuation">:</span>
          	<span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>service
          	<span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
  <span class="token punctuation">-</span> <span class="token key atrule">host</span><span class="token punctuation">:</span> tomcat.itheima.com
    <span class="token key atrule">http</span><span class="token punctuation">:</span>
      <span class="token key atrule">paths</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">path</span><span class="token punctuation">:</span> /
      	<span class="token key atrule">pathType</span><span class="token punctuation">:</span> Prefix
        <span class="token key atrule">backend</span><span class="token punctuation">:</span>
          <span class="token key atrule">service</span><span class="token punctuation">:</span>
          	<span class="token key atrule">name</span><span class="token punctuation">:</span> tomcat<span class="token punctuation">-</span>service
          	<span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8080</span>
</code></pre> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create -f ingress-https.yaml</span>
ingress<span class="token punctuation">.</span>extensions/ingress-https created

<span class="token comment"># 查看</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get ing ingress-https -n dev</span>
NAME            HOSTS                                  ADDRESS         PORTS     AGE
ingress-https   nginx<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com<span class="token punctuation">,</span>tomcat<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com   10<span class="token punctuation">.</span>104<span class="token punctuation">.</span>184<span class="token punctuation">.</span>38   80<span class="token punctuation">,</span> 443   2m42s

<span class="token comment"># 查看详情</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe ing ingress-https -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
TLS:
  tls-secret terminates nginx<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com<span class="token punctuation">,</span>tomcat<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com
Rules:
Host              Path Backends
<span class="token operator">--</span><span class="token operator">--</span>              <span class="token operator">--</span><span class="token operator">--</span> <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
nginx<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com  <span class="token operator">/</span>  nginx-service:80 <span class="token punctuation">(</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>97:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>98:80<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>119:80<span class="token punctuation">)</span>
tomcat<span class="token punctuation">.</span>itheima<span class="token punctuation">.</span>com <span class="token operator">/</span>  tomcat-service:8080<span class="token punctuation">(</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>99:8080<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>117:8080<span class="token punctuation">,</span>10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>120:8080<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token comment"># 下面可以通过浏览器访问https://nginx.itheima.com:31335 和 https://tomcat.itheima.com:31335来查看了</span>
</code></pre>
                </div>
            </body>
            </html>
            